package com.uroboros.spring.guides.rest.employee2;

import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.linkTo;
import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.methodOn;

import java.util.ArrayList;
import java.util.List;
import org.springframework.hateoas.CollectionModel;
import org.springframework.hateoas.EntityModel;
import org.springframework.hateoas.server.RepresentationModelAssembler;
import org.springframework.stereotype.Component;

@Component
public class Employee2ModelAssembler implements RepresentationModelAssembler<Employee2, EntityModel<Employee2>> {

  @Override
  public EntityModel<Employee2> toModel(Employee2 entity) {
    return EntityModel.of(entity,
        linkTo(methodOn(Employee2Controller.class).oneByHypermediaDrivenSimplify(entity.getId())).withSelfRel(),
        linkTo(methodOn(Employee2Controller.class).allByHypermediaDrivenSimplify()).withRel("employee2s"));
  }

  @Override
  public CollectionModel<EntityModel<Employee2>> toCollectionModel(Iterable<? extends Employee2> entities) {
    List<EntityModel<Employee2>> employee2s = new ArrayList<>();
    entities.forEach(employee2 -> employee2s.add(toModel(employee2)));

    return CollectionModel.of(employee2s, linkTo(methodOn(Employee2Controller.class).allByHypermediaDrivenSimplify()).withSelfRel());
  }
}
